package com.ttt.gdfood.mapper;

import com.ttt.gdfood.entity.Contact;
import com.ttt.gdfood.entity.Note;
import org.apache.ibatis.annotations.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Mapper
public interface ContactMapper {
    @Select("SELECT * FROM contact WHERE uid=#{uid}")
    @Results({
            @Result(property = "user", column = "cid", one = @One(select = "com.ttt.gdfood.mapper.UserMapper.findById")),
            @Result(property = "contact_id",column = "contact_id"),
            @Result(property = "cid",column = "cid")
    })
    @Transactional
    List<Contact> getContact(int uid);

    @Select("SELECT contact_id,uid FROM contact WHERE cid=#{cid}")
    @Transactional
    List<Contact> getNoteContact(int cid);

    // 获取关注数
    @Select("SELECT count(uid) FROM contact WHERE uid=#{uid}")
    @Transactional
    int getContactNum(int uid);

    // 获取粉丝数
    @Select("SELECT count(cid) FROM contact WHERE cid=#{cid}")
    @Transactional
    int getFansNum(int cid);

    // 用户加关注
    @Insert("INSERT into `contact` (`uid`, `cid`) VALUES (#{uid},#{cid})")
    @Transactional
    void insertContact(int uid,int cid);

    // 根据联系人id删除
    @Delete("DELETE FROM `contact` WHERE contact_id=#{contact_id}")
    void delContactById(int contact_id);

    // 根据用户关注的用户id删除
    @Delete("DELETE FROM `contact` WHERE cid=#{cid}")
    void delContactByCid(int cid);
}
